package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Given a List of words, return the words
 * that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
 * Example 1:
 * Input: ["Hello", "Alaska", "Dad", "Peace"]
 * Output: ["Alaska", "Dad"]
 */
public class KeyboardRow {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(findWords(new String[]{"Hello", "Alaska", "Dad", "Peace"})));
    }

    public static String[] findWords(String[] words) {
        String[] res;
        List<String> list = new ArrayList<>();
        Character[] r1 = {'q','w','e','r','t','y','u','i','o','p'};
        Character[] r2 = {'a','s','d','f','g','h','j','k','l'};
        Character[] r3 = {'z','x','c','v','b','n','m'};
        List<Character> row1 = Arrays.asList(r1);
        List<Character> row2 = Arrays.asList(r2);
        List<Character> row3 = Arrays.asList(r3);


        for (String s: words) {
            char[] chars = s.toCharArray();

            int row1Count = 0;
            int row2Count = 0;
            int row3Count = 0;

            for (char aChar : chars) {
                if (row1.contains(aChar)) {
                    row1Count++;
                } else if (row2.contains(aChar)) {
                    row2Count++;
                } else if (row3.contains(aChar)) {
                    row3Count++;
                }

                if ((row1Count > 0 && row3Count > 0) | (row2Count > 0 && row3Count > 0) | (row1Count > 0 && row2Count > 0)) {
                    break;
                }
            }
            if((row1Count == 0 && row2Count == 0 && row3Count > 0) | (row1Count == 0 && row2Count > 0 && row3Count == 0) | (row1Count > 0 && row2Count == 0 && row3Count == 0)){
                list.add(s);
            }
        }

        res = new String[list.size()];
        for (int i = 0; i < res.length; i++) {
            res[i] = list.get(i);
        }
        return res;
    }
}
